package com.itheima.app.manager;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Sound;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.SoundService;
import com.itheima.vo.SoundVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.Duration;
import java.util.List;

@Component
public class SoundManager {

    @DubboReference
    private SoundService soundService;

    @Autowired
    private FdfsWebServer webServer;

    @Autowired
    private FastFileStorageClient client;

    @DubboReference
    private UserInfoService userInfoService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;



    public void sendYuyin(MultipartFile soundFile) throws IOException {

        //获取线程内userId
        Long userId = UserHolder.get().getId();
        //上传语音
        StorePath storePath = client.uploadFile(soundFile.getInputStream(), soundFile.getSize(), FileUtil.extName(soundFile.getOriginalFilename()), null);
        String soundUrl = webServer.getWebServerUrl() + storePath.getFullPath();

        //封装sound对象
        Sound sound = new Sound();
        //sound.setId(ObjectId.get());
        sound.setUserId(userId);//用户id
        sound.setSoundUrl(soundUrl);//语音url
        //sound.setRemaining(10);//语音剩余次数 默认是10
        sound.setCreated(System.currentTimeMillis());//发送时间
        sound.setPick(1);//是否被捡取

        //调用rpc保存
        soundService.saveSound(sound);


    }


    //查询语音
    public ResponseEntity acceptSound() {
        //次数
          Integer remaining = 10;

        //获取用户id
        Long userId = UserHolder.get().getId();

        //从redis中查询次数
        //没有 新建次数
        if (!stringRedisTemplate.hasKey("SOUND_" + userId)) {
            stringRedisTemplate.opsForValue().set("SOUND_"+userId,"10",Duration.ofDays(1));
        }
        //有 减次数
        String remaStr = stringRedisTemplate.opsForValue().get("SOUND_" + userId);
        //将字符串转成整型
        remaining = Integer.valueOf(remaStr)-1;
        String s = Integer.toString(remaining);
        //减完放回去
        stringRedisTemplate.opsForValue().set("SOUND_" + userId,s);

        //被传音用户id
        Long id =99l;

        // 查询所有录音
        List<Sound> soundList = soundService.findAll(userId);
        int index = RandomUtil.randomInt(0, soundList.size());
        Sound sound1 = soundList.get(index);

        UserInfo userInfo = userInfoService.findById(sound1.getUserId());
        Sound sound = soundService.findBySoundId(sound1.getUserId());
        SoundVo vo = new SoundVo();
        //设置用户基本信息
        vo.setRemainingTimes(remaining);
        vo.setUserInfo(userInfo);
        //设置语音
        vo.setSoundUrl(sound.getSoundUrl());//

        return ResponseEntity.ok(vo);
    }
}
